// pages/index/video-details/video-details.js
const app = getApp()
let wxParse = require('../../utils/wxParse/wxParse.js');
const common = require('../../utils/login.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    isIpx: app.globalData.isIphoneX ? true : false,
    videoSrc: 'http://wxsnsdy.tc.qq.com/105/20210/snsdyvideodownload?filekey=30280201010421301f0201690402534804102ca905ce620b1241b726bc41dcff44e00204012882540400&bizid=1023&hy=SH&fileparam=302c020101042530230204136ffd93020457e3c4ff02024ef202031e8d7f02030f42400204045a320a0201000400',
    header:{
      title: '他专注家庭和生活 专注平凡的小人物',
      author: 'Meda Hyatt'
    },
    videoDesc: '近藤龙人用Arricam ST、Arriflex 535B等胶片机，搭配多种滤镜进行拍摄，提高画面饱和度，呈现出贴近电影故事的杂乱感。在后期制作时，选用成像柔和的Cine Vivo扫描机，给画面增添清新温润的质感。',
    rank: {
      area: '四川',
      areaRank: 1,
      countryRank: 3,
      tickets: 9980,
    },
    voteImg: '../../image/detailsVoting.png',
    vote: true,
    isPaused: true,
    showCenterPlayBtn: false,
    showBottomPlay: true,
    variable: {
      userBtn: true
    },
    articleInfo: {},
    id: '' // 文章id
  },
  onLoad: function (options) {
    console.log(3);
    this.setData({
      id: options.id
    })
    this.isUserInfo();
    this.getArticleDetail(options.id);
  },
  // 获取文章详情
  getArticleDetail (id) {
    let that = this;
    wx.request({
      url: 'https://m.feawin.com/vote/content/' + id,
      data: '',
      method: 'get',
      header: {
        'content-type': 'application/x-www-form-urlencoded',
        // 'custom': JSON.stringify(header),
        'Authorization': 'Bearer ' + wx.getStorageSync('userInfo')
      },
      success: function (res) {
        console.log(res.data);
        let article = res.data.content;
        wxParse.wxParse('article', 'html', article, that, 0);
        that.setData({
          articleInfo: res.data
        })
      },
      fail: function (res) { },
      complete: function (res) { },
    })
  },
  play: function () {
    this.setData({
      isPaused: false
    })
    this.videoCtx.play()
  },
  // 判断是否有用户信息
  isUserInfo() {
    let userInfo = wx.getStorageSync('userInfo');
    let userBtn = 'variable.userBtn';
    if (userInfo) {
      this.setData({
        [userBtn]: false
      })
    }
  },
  // 登录
  bindGetUserInfo(e) {
    console.log(1);
    let id = this.data.id;
    console.log(id);
    let that = this;
    let userInfo = wx.getStorageSync('userInfo');
    if (!userInfo) {
      common.wxLogin().then(data => {
        console.log(data);
        let userBtn = 'variable.userBtn';
        that.setData({
          token: data.token,
          [userBtn]: false
        })
        wx.setStorageSync('userInfo', data.token); // 保存用户信息
        that.Voting(id);  // 登录之后调用投票
      });
    }
  },
  // 点击投票
  toupiao() {
    console.log(2);
    let id = this.data.id;
    this.Voting(id);
  },
  // 投票请求封装
  Voting: function (id) {
    console.log(id);
    let that = this;
    wx.request({
      url: 'https://m.feawin.com/vote/add/' + id,
      data: '',
      method: 'POST',
      header: {
        'content-type': 'application/x-www-form-urlencoded',
        // 'custom': JSON.stringify(header),
        'Authorization': 'Bearer ' + wx.getStorageSync('userInfo')
      },
      success: function (res) {
        console.log(res)
        wx.showToast({
          title: '投票成功!',
          icon: 'success',
          duration: 2000
        })
        that.getArticleDetail(that.data.id);
      },
      fail: function (res) { },
      complete: function (res) { },
    })
  },
  // 已投票
  Voted() {
    console.log('已投票');
  },
  // DetailVoting: function (e) {
  //   var vote = this.data.vote
  //   var voteImg = this.data.voteImg
  //   if (!vote) {
  //     vote = true
  //     voteImg = '../../image/detailsVoted.png'
  //     this.setData({
  //       vote: vote,
  //       voteImg: voteImg
  //     })
  //     wx.showToast({
  //       title: '投票成功!',
  //       icon: 'success',
  //       duration: 2000
  //     })
  //   }
  // },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    this.videoCtx = wx.createVideoContext('myVideo')
  },

})